package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.entity.Address;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author:YuhuaZhang
 * @Description
 * @Date:Created in 17:31 2021/11/5
 * @Modified By:
 */
public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> selectAddressByUid(int uid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();

        String sql="select a_id as aid,u_id as uid,a_name as aname," +
                "a_phone as aphone,a_detail as adetail,a_state as astate from address where u_id=? order by a_state desc;";

        List<Address> list = queryRunner.query(DBUtil.getConnection(), sql, new BeanListHandler<Address>(Address.class), uid);
        return list;
    }

    @Override
    public void insertAddress(Address address) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();
        String sql="insert into address(u_id,a_name,a_phone,a_detail,a_state) value(?,?,?,?,?)";
        queryRunner.update(DBUtil.getConnection(),sql,address.getUid(),address.getAname(),
                address.getAphone(),address.getAdetail(),address.getAstate());
    }

    @Override
    public void deleteAddressByAid(String aid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();
        String sql="delete from address where a_id=?;";
        queryRunner.update(DBUtil.getConnection(),sql,aid);
    }

    @Override
    public void updateAddressToDefault(String aid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update address set a_state=1 where a_id=?;";
        queryRunner.update(DBUtil.getConnection(),sql,aid);
    }

    @Override
    public void updateAddressToCommons(String aid, int uid) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update address set a_state=0 where a_id!=? and u_id=?;";
        queryRunner.update(DBUtil.getConnection(),sql,aid,uid);
    }

    @Override
    public void updateAddressByAid(Address address) throws SQLException {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update address set a_state=?,a_name=?,a_phone=?,a_detail=? where a_id=? ;";
        queryRunner.update(DBUtil.getConnection(),sql,address.getAstate(),address.getAname(),address.getAphone(),address.getAdetail(),address.getAid());
    }
}
